package com.yida.gwj.config.service.impl;

import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.api.domain.GwjConfFlow;
import com.yida.gwj.api.domain.GwjConfFlowStep;
import com.yida.gwj.config.mapper.GwjConfFlowMapper;
import com.yida.gwj.config.service.IGwjConfFlowService;
import com.yida.gwj.config.service.IGwjConfFlowStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务流程Service业务层处理
 *
 * @author lqq
 * @date 2022-12-13
 */
@Service
public class GwjConfFlowServiceImpl implements IGwjConfFlowService {

	@Autowired
	private GwjConfFlowMapper gcFlowMapper;

	@Autowired
	private IGwjConfFlowStepService gcFlowStepService;


	/**
	 * 查询业务流程列表
	 *
	 * @param gcFlow 业务流程
	 * @return 业务流程
	 */
	@Override
	public List<GwjConfFlow> selectGwjConfFlowList(GwjConfFlow gcFlow) {
		return gcFlowMapper.selectGwjConfFlowList(gcFlow);
	}

	/**
	 * 根据状态查询业务流程列表
	 *
	 * @param status 业务流程
	 * @return 业务流程集合
	 */
	@Override
	public List<GwjConfFlow> selectGwjConfFlowByStatus(String status){
		return gcFlowMapper.selectGwjConfFlowByStatus(status);
	}

	/**
	 * 查询业务步骤
	 *
	 * @param flowId 业务流程主键
	 * @return 业务流程
	 */
	@Override
	public GwjConfFlow selectGwjConfFlowByFlowId(Long flowId) {
		return gcFlowMapper.selectGwjConfFlowByFlowId(flowId);
	}


	/**
	 * 流程绑定步骤
	 * 原方法（saveFlowStep）
	 *
	 * @param flowId    流程步骤编号
	 * @param gcFlowSteps 要绑定的步骤编号集合
	 * @return /
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int saveGwjConfFlowStep(Long flowId, List<GwjConfFlowStep> gcFlowSteps) {
		if(flowId>0) {
			gcFlowStepService.deleteGwjConfFlowStepByFlowId(flowId);
		}
		if(gcFlowSteps.size()>0) {
			gcFlowSteps.forEach(temp -> temp.setFlowId(flowId));
		}
		return gcFlowStepService.batchGwjConfFlowStep(gcFlowSteps);
	}

	/**
	 * 新增业务流程
	 *
	 * @param gcFlow 业务流程
	 * @return 结果
	 */
	@Override
	public int insertGwjConfFlow(GwjConfFlow gcFlow) {
		if(StringUtils.isEmpty(gcFlow.getName())) {
			throw new ServiceException("保存失败，流程名称不能为空！");
		}
		if (StringUtils.isNotNull(gcFlowMapper.selectGwjConfFlowByFlowName(gcFlow.getName()))) {
			throw new ServiceException("保存失败，流程名称已存在！");
		}
		gcFlow.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
		gcFlow.setCreateTime(DateUtils.getNowDate());
		return gcFlowMapper.insertGwjConfFlow(gcFlow);
	}

	/**
	 * 修改业务流程
	 *
	 * @param gcFlow 业务流程
	 * @return 结果
	 */
	@Override
	public int updateGwjConfFlow(GwjConfFlow gcFlow) {
		if(StringUtils.isEmpty(gcFlow.getName())) {
			throw new ServiceException("更新失败，流程名称不能为空！");
		}
		GwjConfFlow temp = gcFlowMapper.selectGwjConfFlowByFlowName(gcFlow.getName());
		if (StringUtils.isNotNull(temp) && !temp.getFlowId().equals(gcFlow.getFlowId())) {
			throw new ServiceException("更新失败，流程名称已存在！");
		}
		gcFlow.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
		gcFlow.setUpdateTime(DateUtils.getNowDate());
		return gcFlowMapper.updateGwjConfFlow(gcFlow);
	}

	/**
	 * 批量删除业务流程
	 *
	 * @param flowIds 需要删除的业务流程主键
	 * @return 结果
	 */
	@Override
	public int deleteByFlowIds(Long[] flowIds) {
		List<GwjConfFlow> list = Arrays.stream(flowIds).map(id -> {
			GwjConfFlow temp = new GwjConfFlow();
			temp.setDelFlag(UserStatus.DELETED.getCode());
			temp.setUpdateTime(DateUtils.getNowDate());
			temp.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
			temp.setFlowId(id);
			return temp;
		}).collect(Collectors.toList());
		return gcFlowMapper.updateBatchGwjConfFlow(list);
	}

	@Override
	public int deleteGwjConfFlowByFlowIds(Long[] flowIds) {
		List<GwjConfFlow> list = Arrays.stream(flowIds).map(id -> {
			GwjConfFlow temp = new GwjConfFlow();
			temp.setDelFlag(UserStatus.DELETED.getCode());
			temp.setUpdateTime(DateUtils.getNowDate());
			temp.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
			temp.setFlowId(id);
			return temp;
		}).collect(Collectors.toList());
		return gcFlowMapper.updateBatchGwjConfFlow(list);
	}

}
